//返回ID对象
var g = function(id){
	return document.getElementById(id);
}

//为字符串增加trim方法，使用正则表达式截取空格
String.prototype.trim = function(){							
	return this.replace(/^\s*/,"").replace(/\s*$/,"");
}

//验证的ID
var cname = g("cname");
var cpwd = g("cpwd");
var rpwd = g("rpwd");
var cphone = g("cphone");
var cemail = g("cemail");
var cidcard = g("cidcard");

//错误提示
var cnamemsg = g("cnamemsg");
var cpwdmsg = g("cpwdmsg");
var rpwdmsg = g("rpwdmsg");
var cphonemsg = g("cphonemsg");
var cemailmsg = g("cemailmsg");
var cidcardmsg = g("cidcardmsg");

//客户验证
function cnameCheck(){				
	var exreg = /(^[A-Za-z0-9]{6,16}$)|(^[\u4E00-\u9FA5]{2,8}$)/;
	
	if(cname.value == null || cname.value.trim() == ""){
		cname.style.border = "1px solid";
		cname.style.borderColor = "red";
		cnamemsg.innerHTML = '<font color="red" size="2px">请输入客户名字</font>';
		return false;					
	}else if(!exreg.test(cname.value)){
		cname.style.border = "1px solid";
		cname.style.borderColor = "red";
		cnamemsg.innerHTML = '<font color="red" size="2px">6-16个字符或2-8个中文</font>';			
		return false;
	}else{
		cname.style.border = "";
		cnamemsg.innerHTML = '<font color="green" size="2px">√</font>';
		return true;
	}
}

//密码验证
function cpwdCheck(){
	var exreg = /^(\w){6,20}$/;					//正则表达式
	
	if(cpwd.value == null || cpwd.value.trim() == ""){	
		cpwd.style.border = "1px solid";
		cpwd.style.borderColor = "red";		
		cpwdmsg.innerHTML = '<font color="red" size="2px">请输入密码</font>';	
		return false;
	}else if(!exreg.test(cpwd.value)){
		cpwd.style.border = "1px solid";
		cpwd.style.borderColor = "red";		
		cpwdmsg.innerHTML = '<font color="red" size="2px">6-20个字符，建议由数字和符合两种以上组合</font>';	
		return false;
	}else{
		cpwd.style.border = "";
		cpwdmsg.innerHTML = '<font color="green" size="2px">√</font>';
		return true;
	}
}

//再次确认密码验证
function rpwdCheck(){     	
	if(rpwd.value == null || rpwd.value.trim() == ""){
		rpwd.style.border = "1px solid";
		rpwd.style.borderColor = "red";	
		rpwdmsg.innerHTML = '<font color="red" size="2px">请再次输入密码</font>';
		return false;
	}else if(cpwd.value != rpwd.value || rpwd.value.trim() == ""){
		rpwd.style.border = "1px solid";
		rpwd.style.borderColor = "red";	
		rpwdmsg.innerHTML = '<font color="red" size="2px">您输入的两次密码不一致，请再次确认</font>';
		return false;
	}else{
		rpwd.style.border = "";	
		rpwdmsg.innerHTML = '<font color="green" size="2px">√</font>';
		return true;
	}
}

//手机验证
function cphoneCheck(){
	var phoneRegex = /^[1][358][0-9]{9}$/;	//正则表达式
	
	if(cphone.value.trim() == "" || cphone.value == null){
		cphone.style.border = "1px solid";
		cphone.style.borderColor = "red";
		cphonemsg.innerHTML = "<font color='red' size='1px'>请填写手机号码！</font>";
		return false;
	}else if(!phoneRegex.test(cphone.value)){
		cphone.style.border = "1px solid";
		cphone.style.borderColor = "red";
		cphonemsg.innerHTML = "<font color='red' size='1px'>您填写的手机号码格式不对！请核对后重新填写</font>";
		return false;
	}else{
		cphone.style.border = "";
		cphonemsg.innerHTML = '<font color="green" size="2px">√</font>'
		return true;
	}
}

//邮箱验证
function cemailCheck(){
	var cemailRegex = /^([a-zA-Z0-9_\.\-])+\@(([a-zA-Z0-9\-])+\.)+([a-zA-Z0-9]{2,4})+$/;	//正则表达式
	
	if(cemail.value.trim() == "" || cemail.value == null){
		cemail.style.border = "1px solid";
		cemail.style.borderColor = "red";
		cemailmsg.innerHTML = "<font color='red' size='1px'>请填写邮箱！</font>";
		return false;
	}else if(!cemailRegex.test(cemail.value)){
		cemail.style.border = "1px solid";
		cemail.style.borderColor = "red";
		cemailmsg.innerHTML = "<font color='red' size='1px'>您填写的邮箱格式不对！请核对后重新填写</font>";
		return false;
	}else{
		cemail.style.border = "";
		cemailmsg.innerHTML = '<font color="green" size="2px">√</font>'
		return true;
	}
}

//身份证验证
function cidcardCheck(){
	var cidcardRegex = /^(^[1-9]\d{7}((0\d)|(1[0-2]))(([0|1|2]\d)|3[0-1])\d{3}$)|(^[1-9]\d{5}[1-9]\d{3}((0\d)|(1[0-2]))(([0|1|2]\d)|3[0-1])((\d{4})|\d{3}[Xx])$)$/;;
		
	if(cidcard.value.trim() == "" || cidcard.value == null){
		cidcard.style.border = "1px solid";
		cidcard.style.borderColor = "red";
		cidcardmsg.innerHTML = "<font color='red' size='1px'>请填写身份证号码！</font>";
		return false;
	}else if(!cidcardRegex.test(cidcard.value)){
		cidcard.style.border = "1px solid";
		cidcard.style.borderColor = "red";
		cidcardmsg.innerHTML = "<font color='red' size='1px'>您填写的身份证格式不对！请核对后重新填写</font>";
		return false;
	}else{
		cidcard.style.border = "";
		cidcardmsg.innerHTML = '<font color="green" size="2px">√</font>'
		return true;
	}
}

function checkAll(){
	return cnameCheck() && cpwdCheck() && rpwdCheck() && cphoneCheck() && cemailCheck() && cidcardCheck();
}

function checkAll2(){
	return cnameCheck() || cpwdCheck() || rpwdCheck() || cphoneCheck() || cemailCheck() || cidcardCheck();
}